﻿Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.Web

Public Class ErrorLog
    Implements IComparable(Of ErrorLog)

    Const DataFile As String = "~/App_Data/ErrorLog.xml"

    Public Property ErrorID As Guid
    Public Property ErrorDate As DateTime
    Public Property Message As String
    Public Property Source As String
    Public Property TargetSiteName As String
    Public Property TargetSiteModule As String
    Public Property ErrorStatus As String

    Public Shared Function GetRecent(ByVal count As Integer) As List(Of ErrorLog)
        Dim _return As New List(Of ErrorLog)
        Dim xdoc As New XmlDocument
        xdoc.Load(HttpContext.Current.Server.MapPath(DataFile))
        Dim x As XmlNodeList = xdoc("Errors").ChildNodes
        For Each n As XmlNode In x
            If n("ErrorStatus").InnerText.ToUpper = "NEW" Then
                _return.Add(Marshall(n))
            End If
        Next
        _return.Sort(CreatedComparison)
        _return.Reverse()
        If _return.Count >= count Then
            _return = _return.GetRange(0, count)
        End If
        Return _return
    End Function
    Public Shared Sub LogErrors(ByVal exc As Exception)
        Try

            Dim m As String = exc.Message
            Dim s As String = exc.Source
            Dim tName As String = exc.TargetSite.Name
            Dim tMod As String = exc.TargetSite.Module.FullyQualifiedName

            Dim xdoc As New XmlDocument
            Dim n As XmlNode
            If Not System.IO.File.Exists(HttpContext.Current.Server.MapPath(DataFile)) Then
                Dim declaration As XmlNode = xdoc.CreateNode(XmlNodeType.XmlDeclaration, Nothing, Nothing)
                xdoc.AppendChild(declaration)
                n = xdoc.CreateNode(XmlNodeType.Element, "Errors", Nothing)
                xdoc.AppendChild(n)
            Else
                xdoc.Load(HttpContext.Current.Server.MapPath(DataFile))
                n = xdoc("Errors")
            End If
            Dim newNode As XmlNode = xdoc.CreateNode(XmlNodeType.Element, "Error", Nothing)
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "ErrorID", Nothing)).InnerText = Guid.NewGuid.ToString
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "ErrorDate", Nothing)).InnerText = Now.ToString
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "Message", Nothing)).InnerText = exc.Message
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "Source", Nothing)).InnerText = exc.Source
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "TargetSiteName", Nothing)).InnerText = exc.TargetSite.Name
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "TargetSiteModule", Nothing)).InnerText = exc.TargetSite.Module.FullyQualifiedName
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "ErrorStatus", Nothing)).InnerText = "NEW"
            'newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "Error", Nothing)).InnerText = ""


            n.AppendChild(newNode)
            xdoc.Save(HttpContext.Current.Server.MapPath(DataFile))
        Catch ex As Exception
            ' eat it
        End Try
    End Sub
    Public Shared Function Delete(ByVal id As Guid) As Boolean
        Dim _return As Boolean = False
        Try
            Dim xdoc As New XmlDocument
            xdoc.Load(HttpContext.Current.Server.MapPath(DataFile))
            For Each n As XmlNode In xdoc("Errors").ChildNodes
                If New Guid(n("ErrorID").InnerText) = id Then
                    xdoc("Errors").RemoveChild(n)
                    Exit For
                End If
            Next
            xdoc.Save(HttpContext.Current.Server.MapPath(DataFile))
            _return = True
        Catch ex As Exception
            ErrorLog.LogErrors(ex)
            _return = False
        End Try
        Return _return
    End Function
    Public Shared Function ClearError(ByVal id As Guid) As Boolean
        Dim _return As Boolean = False
        Try
            Dim xdoc As New XmlDocument
            xdoc.Load(HttpContext.Current.Server.MapPath(DataFile))
            For Each n As XmlNode In xdoc("Errors").ChildNodes
                If New Guid(n("ErrorID").InnerText) = id Then
                    n("ErrorStatus").InnerText = "Cleared"
                    Exit For
                End If
            Next
            xdoc.Save(HttpContext.Current.Server.MapPath(DataFile))
            _return = True
        Catch ex As Exception
            ErrorLog.LogErrors(ex)
            _return = False
        End Try
        Return _return
    End Function
    Private Shared Function Marshall(ByVal n As XmlNode) As ErrorLog
        Dim _return As New ErrorLog
        With _return
            .ErrorID = New Guid(n("ErrorID").InnerText)
            .ErrorDate = CDate(n("ErrorDate").InnerText)
            .Message = n("Message").InnerText
            .Source = n("Source").InnerText
            .TargetSiteName = n("TargetSiteName").InnerText
            .TargetSiteModule = n("TargetSiteModule").InnerText
            .ErrorStatus = n("ErrorStatus").InnerText
        End With
        Return _return
    End Function
    Public Shared CreatedComparison As Comparison(Of ErrorLog) = Function(p1 As ErrorLog, p2 As ErrorLog) p1.ErrorDate.CompareTo(p2.ErrorDate)
    
    Public Function CompareTo(ByVal other As ErrorLog) As Integer Implements System.IComparable(Of ErrorLog).CompareTo
        Return ErrorID.ToString.CompareTo(other.ErrorID.ToString)
    End Function
End Class
